<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=6.3.0">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '6.3.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="lazydoc，一个简单的接口文档生成器。 功能，自动生成接口文档，并为每个接口提供一个测试页面。 实现原理，扫描所有带有@controller注解的类，解析带有@RequestMapping的方法，根据方法签名和自定义标签生成文档 最终生成的效果：点这里  网站只是静态页面，所以大部分按钮是无效的">
<meta name="keywords" content="lazydoc">
<meta property="og:type" content="article">
<meta property="og:title" content="lazydoc使用手册">
<meta property="og:url" content="http://yoursite.com/manual/manual/lazydoc/index.html">
<meta property="og:site_name" content="我的云笔记">
<meta property="og:description" content="lazydoc，一个简单的接口文档生成器。 功能，自动生成接口文档，并为每个接口提供一个测试页面。 实现原理，扫描所有带有@controller注解的类，解析带有@RequestMapping的方法，根据方法签名和自定义标签生成文档 最终生成的效果：点这里  网站只是静态页面，所以大部分按钮是无效的">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/blog/images/1533135896268.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533136547225.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533136578789.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533138903288.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533140055012.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533140767437.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533142230223.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533201045412.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533203394950.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533303784163.png">
<meta property="og:image" content="http://yoursite.com/blog/images/1533303972943.png">
<meta property="og:updated_time" content="2019-02-07T03:42:24.525Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="lazydoc使用手册">
<meta name="twitter:description" content="lazydoc，一个简单的接口文档生成器。 功能，自动生成接口文档，并为每个接口提供一个测试页面。 实现原理，扫描所有带有@controller注解的类，解析带有@RequestMapping的方法，根据方法签名和自定义标签生成文档 最终生成的效果：点这里  网站只是静态页面，所以大部分按钮是无效的">
<meta name="twitter:image" content="http://yoursite.com/blog/images/1533135896268.png">






  <link rel="canonical" href="http://yoursite.com/manual/manual/lazydoc/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>lazydoc使用手册 | 我的云笔记</title>
  






  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?5102824f4157d2cbdb282a1a889d910e";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我的云笔记</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/blog/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/blog/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/blog/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/manual/manual/lazydoc/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我的云笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">lazydoc使用手册
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-07-30 18:01:30" itemprop="dateCreated datePublished" datetime="2018-07-30T18:01:30+08:00">2018-07-30</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-02-07 11:42:24" itemprop="dateModified" datetime="2019-02-07T11:42:24+08:00">2019-02-07</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/blog/categories/manual/" itemprop="url" rel="index"><span itemprop="name">manual</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/blog/manual/manual/lazydoc/" class="leancloud_visitors" data-flag-title="lazydoc使用手册">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>lazydoc，一个简单的接口文档生成器。</p>
<p>功能，自动生成接口文档，并为每个接口提供一个测试页面。</p>
<p>实现原理，扫描所有带有@controller注解的类，解析带有@RequestMapping的方法，根据方法签名和自定义标签生成文档</p>
<p>最终生成的效果：<a href="/blog/demo/lazydoc/main.html">点这里</a>  网站只是静态页面，所以大部分按钮是无效的</p>
<a id="more"></a>
<p>下面就以一个简单的spring-boot工程说一下怎么使用</p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><p>暂时用到的依赖包</p>
<p>jdk：1.8 </p>
<p>spring-boot-starter-web：2.0.3.RELEASE</p>
<p>velocity：1.7</p>
<p>lombok：1.16.22</p>
<p>目前版本算是初稿，因为用到了一些jdk的新功能，所以jdk1.8是必须。</p>
<p>spring-boot只用到了它的Annotation和AnnotationUtils，所以版本没啥要求，引入时scope我用的是provided</p>
<p>lombok版本是随spring-boot走的</p>
<p>velocity，生成页面的，用习惯了，后续可能会换</p>
<h1 id="创建工程"><a href="#创建工程" class="headerlink" title="创建工程"></a>创建工程</h1><h2 id="pom-xml"><a href="#pom-xml" class="headerlink" title="pom.xml"></a>pom.xml</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.example<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">name</span>&gt;</span>demo<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">description</span>&gt;</span>Demo project for Spring Boot<span class="tag">&lt;/<span class="name">description</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.3.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">project.reporting.outputEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.reporting.outputEncoding</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">java.version</span>&gt;</span>1.8<span class="tag">&lt;/<span class="name">java.version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="启动类"><a href="#启动类" class="headerlink" title="启动类"></a>启动类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.SpringApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> wang.wangby.lazydoc.controller.LazyDocController;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloApplication</span>  </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		SpringApplication.run(HelloApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="一个简单的Controller"><a href="#一个简单的Controller" class="headerlink" title="一个简单的Controller"></a>一个简单的Controller</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.ResponseBody;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/hello"</span>)</span><br><span class="line">	<span class="meta">@ResponseBody</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">hello</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"hello world"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h2><p><a href="http://127.0.0.1:8080/hello" target="_blank" rel="noopener">http://127.0.0.1:8080/hello</a></p>
<p><img src="/blog/images/1533135896268.png" alt="1533135896268"></p>
<h1 id="hello-world"><a href="#hello-world" class="headerlink" title="hello world"></a>hello world</h1><h2 id="引入jar包"><a href="#引入jar包" class="headerlink" title="引入jar包"></a>引入jar包</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>wang.wangby<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lazydoc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="重启"><a href="#重启" class="headerlink" title="重启"></a>重启</h2><p>这时候会看到启动日志里面多了三个地址</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">RequestMappingHandlerMapping - Mapped &quot;&#123;[/lazydoc/index]&#125;</span><br><span class="line">RequestMappingHandlerMapping - Mapped &quot;&#123;[/lazydoc/detail/&#123;index&#125;],methods=[GET]&#125;&quot;</span><br><span class="line">RequestMappingHandlerMapping - Mapped &quot;&#123;[/lazydoc/treeView],methods=[GET || POST],produces=[application/json;charset=UTF-8]&#125;</span><br></pre></td></tr></table></figure>
<h2 id="访问"><a href="#访问" class="headerlink" title="访问"></a>访问</h2><p>打开这个页面，就可以看到生成的文档了：<a href="http://127.0.0.1:8080/lazydoc/index" target="_blank" rel="noopener">http://127.0.0.1:8080/lazydoc/index</a></p>
<p><img src="/blog/images/1533136547225.png" alt="1533136547225"></p>
<p>点击测试，可以看到返回内容和网页上的一样</p>
<p><img src="/blog/images/1533136578789.png" alt="1533136578789"></p>
<h1 id="0配置"><a href="#0配置" class="headerlink" title="0配置"></a>0配置</h1><p>上面的hello方法，没有参数，返回值也是简单的字符串，所以看不出什么。为了演示，我这里简单的模拟了一个查询的场景。</p>
<p>客户端传过来一些查询条件和分页信息，服务端返回封装好的一页查询结果。</p>
<h2 id="model类"><a href="#model类" class="headerlink" title="model类"></a>model类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//图书信息</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookInfo</span> </span>&#123;</span><br><span class="line">	<span class="comment">//主键</span></span><br><span class="line">	<span class="keyword">private</span> Long bookId;	</span><br><span class="line">	<span class="comment">//书名</span></span><br><span class="line">	<span class="keyword">private</span> String bookName;</span><br><span class="line">	<span class="comment">//出版日期</span></span><br><span class="line">	<span class="keyword">private</span> Date publication;</span><br><span class="line">	<span class="comment">//标准图书编号</span></span><br><span class="line">	<span class="keyword">private</span> String isbn;</span><br><span class="line">	<span class="comment">//创建时间</span></span><br><span class="line">	<span class="keyword">private</span> Date createTime;</span><br><span class="line">	<span class="comment">//售价</span></span><br><span class="line">	<span class="keyword">private</span> Integer price;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="封装查询条件的对象"><a href="#封装查询条件的对象" class="headerlink" title="封装查询条件的对象"></a>封装查询条件的对象</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//图书查询条件</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookSelector</span> </span>&#123;</span><br><span class="line">	<span class="comment">//图书信息</span></span><br><span class="line">	<span class="keyword">private</span> BookInfo bookInfo;</span><br><span class="line">	<span class="comment">//最低价格</span></span><br><span class="line">	<span class="keyword">private</span> Integer minPrice;</span><br><span class="line">	<span class="comment">//最高价</span></span><br><span class="line">	<span class="keyword">private</span> Integer maxPrice;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="封装返回结果的对象"><a href="#封装返回结果的对象" class="headerlink" title="封装返回结果的对象"></a>封装返回结果的对象</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Page</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">	<span class="comment">//返回的记录集</span></span><br><span class="line">	<span class="keyword">private</span> List&lt;T&gt; result;</span><br><span class="line">	<span class="comment">//总条数</span></span><br><span class="line">	<span class="keyword">private</span> Integer total;</span><br><span class="line">	<span class="comment">//查询起始位置</span></span><br><span class="line">	Integer offset;</span><br><span class="line">	<span class="comment">//返回条数</span></span><br><span class="line">	Integer limit;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Controller"><a href="#Controller" class="headerlink" title="Controller"></a>Controller</h2><p>这里模拟返回10条</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/book"</span>)</span><br><span class="line"><span class="comment">//图书管理</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 查询图书信息</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> selector 查询条件</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> offset 查询</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> limit 返回条数</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@return</span> 查询到的结果集</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/select"</span>)</span><br><span class="line">	<span class="meta">@ResponseBody</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Page&lt;BookInfo&gt; <span class="title">select</span><span class="params">(BookSelector selector, Integer offset, Integer limit)</span> </span>&#123;</span><br><span class="line">		Page page = <span class="keyword">new</span> Page();</span><br><span class="line">		List list=<span class="keyword">new</span> ArrayList();</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;<span class="number">10</span>;i++) &#123;</span><br><span class="line">			BookInfo inf=<span class="keyword">new</span> BookInfo();</span><br><span class="line">			inf.setCreateTime(<span class="keyword">new</span> Date());</span><br><span class="line">			inf.setBookId(i+<span class="number">0L</span>);</span><br><span class="line">			inf.setBookName(<span class="string">"bookName:"</span>+i);</span><br><span class="line">			inf.setIsbn(<span class="string">"isbn"</span>+i);</span><br><span class="line">			list.add(inf);</span><br><span class="line">		&#125;</span><br><span class="line">		page.setResult(list);</span><br><span class="line">		page.setOffset(<span class="number">0</span>);</span><br><span class="line">		page.setLimit(<span class="number">10</span>);</span><br><span class="line">		page.setTotal(<span class="number">1000</span>);</span><br><span class="line">		<span class="keyword">return</span> page;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="启动并访问"><a href="#启动并访问" class="headerlink" title="启动并访问"></a>启动并访问</h2><p>在没有任何配置的情况下，文档也生成了，查看网页版：<a href="/blog/demo/lazydoc/zero.html">点这里</a></p>
<p><img src="/blog/images/1533138903288.png" alt="1533138903288"></p>
<h1 id="完善文档"><a href="#完善文档" class="headerlink" title="完善文档"></a>完善文档</h1><p>可以看到，按正常方式写代码，没有做任何配置的情况下，接口文档已经生成了。</p>
<p>下面来看看如何完善文档，其实完善就是把说明字段写清楚就好了，lazydoc的所有配置信息均采用注解方式</p>
<h2 id="参数和返回值对象"><a href="#参数和返回值对象" class="headerlink" title="参数和返回值对象"></a>参数和返回值对象</h2><p>只需要将普通的备注换成@remark便签即可，像这样</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Remark</span>(<span class="string">"图书信息"</span>)</span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookInfo</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Remark</span>(<span class="string">"主键"</span>)</span><br><span class="line">	<span class="keyword">private</span> Long bookId;</span><br><span class="line">	<span class="meta">@Remark</span>(<span class="string">"书名"</span>)</span><br><span class="line">	<span class="keyword">private</span> String bookName;</span><br><span class="line">	<span class="meta">@Remark</span>(<span class="string">"出版日期"</span>)</span><br><span class="line">	<span class="keyword">private</span> Date publication;</span><br><span class="line">	<span class="meta">@Remark</span>(<span class="string">"标准图书编号"</span>)</span><br><span class="line">	<span class="keyword">private</span> String isbn;</span><br><span class="line">	<span class="meta">@Remark</span>(<span class="string">"创建时间"</span>)</span><br><span class="line">	<span class="keyword">private</span> Date createTime;</span><br><span class="line">	<span class="meta">@Remark</span>(<span class="string">"售价"</span>)</span><br><span class="line">	<span class="keyword">private</span> Integer price;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="controller方法"><a href="#controller方法" class="headerlink" title="controller方法"></a>controller方法</h2><p>将原来的注释换个形式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Remark</span>(<span class="string">"查询图书信息"</span>)</span><br><span class="line"><span class="meta">@Param</span>(<span class="string">"查询条件"</span>)</span><br><span class="line"><span class="meta">@Param</span>(<span class="string">"查询起始位置偏移量"</span>)</span><br><span class="line"><span class="meta">@Param</span>(<span class="string">"返回条数"</span>)</span><br><span class="line"><span class="meta">@Return</span>(<span class="string">"查询结果"</span>)</span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/select"</span>)</span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Page&lt;BookInfo&gt; <span class="title">select</span><span class="params">(BookSelector selector, Integer offset, Integer limit)</span> </span>&#123;</span><br><span class="line">    Page page = <span class="keyword">new</span> Page();</span><br><span class="line">    List list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">        BookInfo inf = <span class="keyword">new</span> BookInfo();</span><br><span class="line">        inf.setCreateTime(<span class="keyword">new</span> Date());</span><br><span class="line">        inf.setBookId(i + <span class="number">0L</span>);</span><br><span class="line">        inf.setBookName(<span class="string">"bookName:"</span> + i);</span><br><span class="line">        inf.setIsbn(<span class="string">"isbn"</span> + i);</span><br><span class="line">        inf.setPrice(i * <span class="number">100</span> + i);</span><br><span class="line">        list.add(inf);</span><br><span class="line">    &#125;</span><br><span class="line">    page.setResult(list);</span><br><span class="line">    page.setOffset(<span class="number">0</span>);</span><br><span class="line">    page.setLimit(<span class="number">10</span>);</span><br><span class="line">    page.setTotal(<span class="number">1000</span>);</span><br><span class="line">    <span class="keyword">return</span> page;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以看到，相比于原来的，代码量并没有增多</p>
<p><img src="/blog/images/1533140055012.png" alt="1533140024349"></p>
<h2 id="再次启动"><a href="#再次启动" class="headerlink" title="再次启动"></a>再次启动</h2><p>可以看到备注信息已经显示在页面了，网页版：<a href="/blog/demo/lazydoc/remark.html">点这里</a></p>
<p><img src="/blog/images/1533140767437.png" alt="1533140767437"></p>
<h1 id="接口测试"><a href="#接口测试" class="headerlink" title="接口测试"></a>接口测试</h1><h2 id="设置日期格式"><a href="#设置日期格式" class="headerlink" title="设置日期格式"></a>设置日期格式</h2><p>为了处理日期，需要给spring添加一个Converter</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringToDateConverter</span> <span class="keyword">implements</span> <span class="title">Converter</span>&lt;<span class="title">String</span>, <span class="title">Date</span>&gt; </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String dateFormat = <span class="string">"yyyy-MM-dd HH:mm:ss"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String shortDateFormat = <span class="string">"yyyy-MM-dd"</span>;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Date <span class="title">convert</span><span class="params">(String value)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (StringUtils.isEmpty(value)) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		value = value.trim();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="keyword">return</span> convertInternal(value);</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">			<span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"将字符串转为日期失败:"</span> + value + <span class="string">","</span> + ex.getMessage());</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> Date <span class="title">convertInternal</span><span class="params">(String value)</span> <span class="keyword">throws</span> ParseException </span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (value.matches(<span class="string">"^\\d+$"</span>)) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">new</span> Date(Long.parseLong(value));</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (value.contains(<span class="string">":"</span>)) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">new</span> SimpleDateFormat(dateFormat).parse(value);</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">new</span> SimpleDateFormat(shortDateFormat).parse(value);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>修改application.properties</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">spring.jackson.date-format=yyyy-MM-dd HH:mm:ss</span><br><span class="line">spring.jackson.time-zone=GMT+8</span><br></pre></td></tr></table></figure>
<h2 id="测试接收参数"><a href="#测试接收参数" class="headerlink" title="测试接收参数"></a>测试接收参数</h2><p>将select方法稍微改下，将接受到的参数原封传回页面</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Page page = <span class="keyword">new</span> Page();</span><br><span class="line">List list = <span class="keyword">new</span> ArrayList();</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; limit; i++) &#123;</span><br><span class="line">    BookInfo inf = selector.getBookInfo();</span><br><span class="line">    inf.setPrice(selector.getMaxPrice());</span><br><span class="line">    list.add(inf);</span><br><span class="line">&#125;</span><br><span class="line">page.setResult(list);</span><br><span class="line">page.setOffset(offset);</span><br><span class="line">page.setLimit(limit);</span><br><span class="line">page.setTotal(<span class="number">1000</span>);</span><br><span class="line"><span class="keyword">return</span> page;</span><br></pre></td></tr></table></figure>
<h2 id="启动并访问-1"><a href="#启动并访问-1" class="headerlink" title="启动并访问"></a>启动并访问</h2><p>网页版：<a href="/blog/demo/lazydoc/test.html">点这里</a></p>
<p><img src="/blog/images/1533142230223.png" alt="1533142230223"></p>
<h1 id="参数配置"><a href="#参数配置" class="headerlink" title="参数配置"></a>参数配置</h1><h2 id="忽略参数"><a href="#忽略参数" class="headerlink" title="忽略参数"></a>忽略参数</h2><p>有事为了省事，把model对象直接放到查询对象里了，但有些查询条件是没用的，可以设置ignores，这样页面就不会显示了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Param</span>(value=<span class="string">"查询条件"</span>,ignores= &#123;<span class="string">"bookInfo.bookId"</span>,<span class="string">"bookInfo.createTime"</span>,<span class="string">"bookInfo.price"</span>,<span class="string">"bookInfo.publication"</span>&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="参数校验"><a href="#参数校验" class="headerlink" title="参数校验"></a>参数校验</h2><h3 id="默认配置"><a href="#默认配置" class="headerlink" title="默认配置"></a>默认配置</h3><p>根据数据对应的java类型，默认已经有参数校验了，比如日期和数字。</p>
<p>date类型默认是按YYYY-MM-DD hh:mm:ss校验的，如果只有日期没有时间，在字段上做一些配置</p>
<p>@Property注解，dateOnly参数判断是时间还是日期</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Property</span>(value=<span class="string">"出版日期"</span>,dateOnly=<span class="keyword">true</span>)</span><br><span class="line"><span class="keyword">private</span> Date publication;</span><br></pre></td></tr></table></figure>
<p><img src="/blog/images/1533201045412.png" alt="1533199895389"></p>
<h3 id="非空"><a href="#非空" class="headerlink" title="非空"></a>非空</h3><p>notNull属性设置为true，提交时就会自动校验了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Param</span>(value=<span class="string">"查询起始位置偏移量"</span>,notNull=<span class="keyword">true</span>)</span><br></pre></td></tr></table></figure>
<p>最终效果：<a href="/blog/demo/lazydoc/main.html">点这里</a></p>
<h1 id="json形式的参数"><a href="#json形式的参数" class="headerlink" title="json形式的参数"></a>json形式的参数</h1><p>微服务下，接口的参数通常是通过json的方式传递，如果在方法上加了@RequestBody 注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Remark</span>(<span class="string">"通过json查询图书"</span>)</span><br><span class="line"><span class="meta">@Param</span>(value=<span class="string">"查询条件"</span>)</span><br><span class="line"><span class="meta">@Param</span>(value=<span class="string">"查询起始位置偏移量"</span>)</span><br><span class="line"><span class="meta">@Param</span>(value=<span class="string">"返回条数"</span>)</span><br><span class="line"><span class="meta">@Return</span>(<span class="string">"查询结果"</span>)</span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/selectJs"</span>)</span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Page&lt;BookInfo&gt; <span class="title">selectJs</span><span class="params">(@RequestBody BookSelector selector)</span> </span>&#123;</span><br><span class="line">    Page page = <span class="keyword">new</span> Page();</span><br><span class="line">    List list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; selector.getLimit(); i++) &#123;</span><br><span class="line">        BookInfo inf = selector.getBookInfo();</span><br><span class="line">        inf.setPrice(selector.getMaxPrice());</span><br><span class="line">        list.add(inf);</span><br><span class="line">    &#125;</span><br><span class="line">    page.setResult(list);</span><br><span class="line">    page.setOffset(selector.getOffset());</span><br><span class="line">    page.setLimit(selector.getLimit());</span><br><span class="line">    page.setTotal(<span class="number">1000</span>);</span><br><span class="line">    <span class="keyword">return</span> page;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>那么测试页面就会自动变为json方式输入了，同时会把需要的字段自动设置为null，例如这样</p>
<p><img src="/blog/images/1533203394950.png" alt="1533203394950"></p>
<p>json格式的数据目前还无法做到参数校验，同时只要有1个参数上标注了RequestBody ，页面就会使用json形式</p>
<h1 id="mock"><a href="#mock" class="headerlink" title="mock"></a>mock</h1><p>通常的开发流程是，各部门之间定好接口，然后各自独立开发。当各方进度不一样的时候，为了方便别人测试，需要对方提供一个可供测试的接口。</p>
<p>我的实现方式是，将模拟的返回数据存在缓存中，请求时通过一个唯一标识来获取模拟数据。</p>
<p>暂时做法是将数据放在redis里，为了实现mock功能需要做以下配置</p>
<h2 id="引入新的jar包"><a href="#引入新的jar包" class="headerlink" title="引入新的jar包"></a>引入新的jar包</h2><p>api-cache：用于将请求内容放入缓存，项目地址<a href="https://gitee.com/xixhaha/apicache" target="_blank" rel="noopener">https://gitee.com/xixhaha/apicache</a></p>
<p>spring-boot-starter-aop：通过AOP判断哪些方法需要做mock</p>
<p>spring-boot-starter-data-redis：数据存在redis</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>wang.wangby<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>api-cache<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-aop<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="修改Controller方法注解"><a href="#修改Controller方法注解" class="headerlink" title="修改Controller方法注解"></a>修改Controller方法注解</h2><p>这里采用annotation的方式，新增了一个注解Api，这个方内部同时引用了ResponseBody，RequestMapping，Remark</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Target</span>(ElementType.METHOD)</span><br><span class="line"><span class="meta">@Retention</span>(RetentionPolicy.RUNTIME)</span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping</span></span><br><span class="line"><span class="meta">@Remark</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> Api &#123;...</span><br></pre></td></tr></table></figure>
<p>所以可以把三个注解去掉，换成下面这样</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Param</span>(value=<span class="string">"查询条件"</span>,ignores= &#123;<span class="string">"bookInfo.createTime"</span>,<span class="string">"bookInfo.price"</span>,<span class="string">"bookInfo.publication"</span>,<span class="string">"bookInfo.isbn"</span>,</span><br><span class="line">                              <span class="string">"minPrice"</span>,<span class="string">"maxPrice"</span>&#125;)</span><br><span class="line"><span class="meta">@Return</span>(<span class="string">"查询结果"</span>)</span><br><span class="line"><span class="meta">@Api</span>(name=<span class="string">"查询图书信息"</span>,path=<span class="string">"/select"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> Page&lt;BookInfo&gt; <span class="title">select</span><span class="params">(BookSelector selector)</span> </span>&#123;</span><br><span class="line">    Page page = <span class="keyword">new</span> Page();</span><br><span class="line">    List list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; selector.getLimit(); i++) &#123;</span><br><span class="line">        BookInfo b=<span class="keyword">new</span> BookInfo();</span><br><span class="line">        b.setBookId(i+<span class="number">0L</span>);</span><br><span class="line">        b.setBookName(selector.getBookInfo().getBookName()+i);</span><br><span class="line">        b.setCreateTime(<span class="keyword">new</span> Date());</span><br><span class="line">        list.add(b);</span><br><span class="line">    &#125;</span><br><span class="line">    page.setResult(list);</span><br><span class="line">    page.setOffset(selector.getOffset());</span><br><span class="line">    page.setLimit(selector.getLimit());</span><br><span class="line">    page.setTotal(<span class="number">1000</span>);</span><br><span class="line">    <span class="keyword">return</span> page;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="配置AOP"><a href="#配置AOP" class="headerlink" title="配置AOP"></a>配置AOP</h2><p>接口调用的时候会出现一些异常，同时当多台服务器做集群的时候，由于网络异常需要重试，有可能出现重复请求的情况，出现这些情况如何处理，专门设置了一个类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** 设置mock异常处理规则 */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ApiMock</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 当其他服务器正在执行方法的时候,接口调用的返回值</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> method 请求的方法</span></span><br><span class="line"><span class="comment">	 * */</span> </span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">getRunningResult</span><span class="params">(Method method)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 当调用方法出现异常后,接口的返回值</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> method 请求的方法</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> ex     异常信息</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">getExceptionResult</span><span class="params">(Method method, Exception ex)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 获得请求的唯一标识</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> method 请求方法</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> args   方法参数</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getRequestId</span><span class="params">(Method method, Object[] args)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>客户端设置拦截规则的同时，需要实现以上接口，最终的配置类如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MockConfig</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Bean</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> MockMethod <span class="title">apiMethods</span><span class="params">(CacheServer cacheServer)</span> </span>&#123;</span><br><span class="line">		MockMethod method=<span class="keyword">new</span>  MockMethod();</span><br><span class="line">		method.setCacheServer(cacheServer);</span><br><span class="line">		<span class="keyword">return</span> method;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Aspect</span></span><br><span class="line">	<span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MockMethod</span> <span class="keyword">implements</span> <span class="title">ApiMock</span></span>&#123;</span><br><span class="line">		CacheServer cacheServer;</span><br><span class="line">		</span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCacheServer</span><span class="params">(CacheServer cacheServer)</span> </span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.cacheServer=cacheServer;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//只处理有Api注解的方法，同时返回值必须是Page</span></span><br><span class="line">		<span class="meta">@Pointcut</span>(<span class="string">"@annotation(wang.wangby.annotation.Api) &amp;&amp; execution(com.example.demo.book.model.Page *.*(..))"</span>)</span><br><span class="line">		<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">apiMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line"></span><br><span class="line">		<span class="meta">@Around</span>(<span class="string">"apiMethod()"</span>)</span><br><span class="line">		<span class="function"><span class="keyword">public</span> Object <span class="title">process</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">new</span> ApiIntercept(pjp,cacheServer).process(<span class="keyword">this</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> Object <span class="title">getRunningResult</span><span class="params">(Method method)</span> </span>&#123;</span><br><span class="line">			<span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"重复请求"</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果请求出现异常,打印异常,并造一个假的Page</span></span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> Object <span class="title">getExceptionResult</span><span class="params">(Method method, Exception ex)</span> </span>&#123;</span><br><span class="line">			log.error(ex.getMessage(),ex);</span><br><span class="line">			Page&lt;BookInfo&gt; page=<span class="keyword">new</span> Page();</span><br><span class="line">			BookInfo bk=<span class="keyword">new</span> BookInfo();</span><br><span class="line">			bk.setBookName(<span class="string">"查询出错:"</span>+ex.getMessage());</span><br><span class="line">			List list=<span class="keyword">new</span> ArrayList();</span><br><span class="line">			list.add(bk);</span><br><span class="line">			page.setResult(list);</span><br><span class="line">			<span class="keyword">return</span> page;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//通过bookId做为标识</span></span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> String <span class="title">getRequestId</span><span class="params">(Method method, Object[] args)</span> </span>&#123;</span><br><span class="line">			BookSelector sel=(BookSelector) args[<span class="number">0</span>];</span><br><span class="line">			String requestId=sel.getBookInfo().getBookId()+<span class="string">""</span>;</span><br><span class="line">			log.debug(<span class="string">"收到请求:"</span>+requestId);</span><br><span class="line">			<span class="keyword">return</span> requestId;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="配置redis"><a href="#配置redis" class="headerlink" title="配置redis"></a>配置redis</h2><p>修改application.yaml</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span> </span><br><span class="line"><span class="attr">  redis:</span></span><br><span class="line"><span class="attr">    database:</span> <span class="number">0</span></span><br><span class="line"><span class="attr">    host:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></span><br><span class="line"><span class="attr">    port:</span> <span class="number">6379</span></span><br></pre></td></tr></table></figure>
<h2 id="启动程序"><a href="#启动程序" class="headerlink" title="启动程序"></a>启动程序</h2><p>bookId设置为123456</p>
<p><img src="/blog/images/1533303784163.png" alt="1533303784163"></p>
<p>查询一次以后，只要参数里的bookId是123456，其它条件无论写什么返回的都是相同内容，同时redis里面多了一些数据，箭头标注的地方就是请求的唯一标识了，同时可以任意修改里面的内容，这样就可以造出不同的mock数据</p>
<p><img src="/blog/images/1533303972943.png" alt="1533303925536"></p>

      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/lazydoc/" rel="tag"># lazydoc</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/code/springboot/springboot/" rel="next" title="springboot初探">
                <i class="fa fa-chevron-left"></i> springboot初探
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/code/spring/springClould/" rel="prev" title="springcloud">
                springcloud <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/blog/archives">
                
                    <span class="site-state-item-count">54</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/blog/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">25</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/blog/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">45</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#运行环境"><span class="nav-number">1.</span> <span class="nav-text">运行环境</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#创建工程"><span class="nav-number">2.</span> <span class="nav-text">创建工程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#pom-xml"><span class="nav-number">2.1.</span> <span class="nav-text">pom.xml</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动类"><span class="nav-number">2.2.</span> <span class="nav-text">启动类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一个简单的Controller"><span class="nav-number">2.3.</span> <span class="nav-text">一个简单的Controller</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运行"><span class="nav-number">2.4.</span> <span class="nav-text">运行</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#hello-world"><span class="nav-number">3.</span> <span class="nav-text">hello world</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#引入jar包"><span class="nav-number">3.1.</span> <span class="nav-text">引入jar包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#重启"><span class="nav-number">3.2.</span> <span class="nav-text">重启</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#访问"><span class="nav-number">3.3.</span> <span class="nav-text">访问</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0配置"><span class="nav-number">4.</span> <span class="nav-text">0配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#model类"><span class="nav-number">4.1.</span> <span class="nav-text">model类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#封装查询条件的对象"><span class="nav-number">4.2.</span> <span class="nav-text">封装查询条件的对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#封装返回结果的对象"><span class="nav-number">4.3.</span> <span class="nav-text">封装返回结果的对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Controller"><span class="nav-number">4.4.</span> <span class="nav-text">Controller</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动并访问"><span class="nav-number">4.5.</span> <span class="nav-text">启动并访问</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#完善文档"><span class="nav-number">5.</span> <span class="nav-text">完善文档</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#参数和返回值对象"><span class="nav-number">5.1.</span> <span class="nav-text">参数和返回值对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#controller方法"><span class="nav-number">5.2.</span> <span class="nav-text">controller方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#再次启动"><span class="nav-number">5.3.</span> <span class="nav-text">再次启动</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#接口测试"><span class="nav-number">6.</span> <span class="nav-text">接口测试</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#设置日期格式"><span class="nav-number">6.1.</span> <span class="nav-text">设置日期格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#测试接收参数"><span class="nav-number">6.2.</span> <span class="nav-text">测试接收参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动并访问-1"><span class="nav-number">6.3.</span> <span class="nav-text">启动并访问</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参数配置"><span class="nav-number">7.</span> <span class="nav-text">参数配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#忽略参数"><span class="nav-number">7.1.</span> <span class="nav-text">忽略参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参数校验"><span class="nav-number">7.2.</span> <span class="nav-text">参数校验</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#默认配置"><span class="nav-number">7.2.1.</span> <span class="nav-text">默认配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#非空"><span class="nav-number">7.2.2.</span> <span class="nav-text">非空</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#json形式的参数"><span class="nav-number">8.</span> <span class="nav-text">json形式的参数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#mock"><span class="nav-number">9.</span> <span class="nav-text">mock</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#引入新的jar包"><span class="nav-number">9.1.</span> <span class="nav-text">引入新的jar包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改Controller方法注解"><span class="nav-number">9.2.</span> <span class="nav-text">修改Controller方法注解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#配置AOP"><span class="nav-number">9.3.</span> <span class="nav-text">配置AOP</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#配置redis"><span class="nav-number">9.4.</span> <span class="nav-text">配置redis</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动程序"><span class="nav-number">9.5.</span> <span class="nav-text">启动程序</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"></span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Pisces</a> v6.3.0</div>





<span id="busuanzi_container_site_uv">
  &nbsp;<span id="busuanzi_value_site_uv"></span>
</span>

<span>/</span>

<span id="busuanzi_container_site_pv">
    <span id="busuanzi_value_site_pv"></span>
</span>


        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=6.3.0"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=6.3.0"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=6.3.0"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=6.3.0"></script>



  



  










  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("CIBUz5A0KNFv8xbAPO8ncqk6-gzGzoHsz", "YtGifAKqwENTTs9hhQ1GxdAG");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            
            counter.save(null, {
              success: function(counter) {
                
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(counter.get('time'));
                
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            
              var newcounter = new Counter();
              /* Set ACL */
              var acl = new AV.ACL();
              acl.setPublicReadAccess(true);
              acl.setPublicWriteAccess(true);
              newcounter.setACL(acl);
              /* End Set ACL */
              newcounter.set("title", title);
              newcounter.set("url", url);
              newcounter.set("time", 1);
              newcounter.save(null, {
                success: function(newcounter) {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
                },
                error: function(newcounter, error) {
                  console.log('Failed to create');
                }
              });
            
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  
  

  

  

  

  

  

</body>
</html>
